package com.app.web.adimin.model;

import java.util.List;

import com.app.util.JDBC;
import com.app.web.BaseModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class Template extends BaseModel<Template>{

	private static final long serialVersionUID = 186684748419693891L;
	
	public static final Template dao = new Template();
	public static final JDBC jdbc=new JDBC();
	
	public int deleteByIds(String ids){
		return Db.update("delete from template where id in ("+ids+")");
	}
	
	public List<Template> findByIds(String ids,String modelName){
		List<Template> templates = dao.find("select '"+modelName+"' as model,a.* from template a where a.id in ("+ids+")");
		return templates;
	}
	/**
	 * 获取表结构图
	 * @param table
	 * @return
	 */
	public String findByTableStrtus(String table){
		
		//列长度				15-1				14-1		  6-1	5-1		9-1			16-1			25-1	
		String strtus =   "+---------------+--------------+------+-----+---------+----------------+-------------------------+\n";
			   strtus+=" * | Field         | Type         | Null | Key | Default | Extra          | Comment                 |\n";
		       strtus+=" * +---------------+--------------+------+-----+---------+----------------+-------------------------+\n";
		
		try{
			//list=Db.find("desc myapp."+table);
			List<Record> list=Db.find("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='myapp' AND TABLE_NAME=?",table);
			
			for(Record tab : list){
				String Field=tab.get("COLUMN_NAME", "");
				String Type=tab.get("DATA_TYPE", "");
				String Null=tab.get("IS_NULLABLE", "");
				String Key=tab.get("COLUMN_KEY", "");
				String Default=tab.get("COLUMN_DEFAULT", "");
				String Extra=tab.get("EXTRA", "");
				String Comment=tab.get("COLUMN_COMMENT", "");
				
				strtus+=" * | "+Field+getSpace(14-Field.length())+"| " +Type+getSpace(13-Type.length())+"| "
					  + Null+getSpace(5-Null.length())+"| " +Key+getSpace(4-Key.length())+"| "
					  + Default+getSpace(8-Default.length())+"| " +Extra+getSpace(15-Extra.length())+"| "
					  + Comment+getSpace(24-getStrLength(Comment))+"|\n";
			}
			
			  strtus+=" * +---------------+--------------+------+-----+---------+----------------+-------------------------+";
		}catch(Exception e){
			e.printStackTrace();
		}
			   
		return strtus;
	}
	
	/**
	 * 得到空格
	 */
	public String getSpace(int num){
		StringBuffer sb=new StringBuffer("");
		if(num>0){
			for(int i=0;i<num;i++){
				sb.append(" ");
			}
		}
		return sb.toString();
	}
	/**
	 * 判断中英文  (中：false,英：true)
	 */
	public boolean isLetter(char c) { 
       int k = 0x80;   
       return c / k == 0 ? true : false;   
	} 
	/**
	 * 得到字符串占位，中文2位，英文1位
	 */
	public int getStrLength(String s) {  
       if (s == null)  
           return 0;  
       char[] c = s.toCharArray();  
       int len = 0;  
       for (int i = 0; i < c.length; i++) {  
           len++;  
           if (!isLetter(c[i])) {  
               len++;  
           }  
       }  
       return len;  
   }  
}
